﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace PassBag.Presentation.Controls {
    public partial class StartPageControl : UserControl {
        private const int NB_RECENT_FILES = 5;
        // delegate declaration
        public delegate void OpenFileEventHandler(object sender, FileEventArgs e);

        // event declaration
        [DescriptionAttribute("Handle the select file")]
        [CategoryAttribute("Recent Category")]
        [BrowsableAttribute(true)]
        [DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Visible)]
        public event OpenFileEventHandler QueryFileOpen;

        public string this[int index] {
            set {
                if (index >= NB_RECENT_FILES || index < 0) {
                    throw new OverflowException (string.Format (Properties.Resources.StartPageControl_FileCountError, NB_RECENT_FILES));
                }

                SetRecentFile(index+1, value);
            }
        }

        public StartPageControl() {
            InitializeComponent();

            Clear();
        }

        private void SetRecentFile(int index, string text) {
            switch (index) {
                case 1:
                    SetLinkText(recentFile1Link, text);
                    break;

                case 2:
                    SetLinkText(recentFile2Link, text);
                    break;

                case 3:
                    SetLinkText(recentFile3Link, text);
                    break;

                case 4:
                    SetLinkText(recentFile4Link, text);
                    break;

                case 5:
                    SetLinkText(recentFile5Link, text);
                    break;

            }
        }

        private void SetLinkText(LinkLabel link, string text) {
            if (string.IsNullOrEmpty(text))	{
                link.Visible = false;
                link.Text = string.Empty;
        	}
            else {
                link.Visible = true;
                link.Text = text;
            }
        }

        public void Clear() {
            for (int i = 0; i < NB_RECENT_FILES; i++) {
                SetRecentFile(i, string.Empty);
            }
        }

        private void recentFile1Link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
            FireEvent(recentFile1Link.Text);
        }

        private void recentFile2Link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
            FireEvent(recentFile2Link.Text);
        }

        private void recentFile3Link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
            FireEvent(recentFile3Link.Text);
        }

        private void recentFile4Link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
            FireEvent(recentFile4Link.Text);
        }

        private void recentFile5Link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
            FireEvent(recentFile5Link.Text);
        }

        private void FireEvent(string fileName) {
            FileEventArgs pass = new FileEventArgs("File open", fileName);
            QueryFileOpen(this, pass);
        }

        public override void Refresh() {
            // recent file list is empty
            if (string.IsNullOrEmpty(recentFile1Link.Text)) {
                titleLabel.Text = Properties.Resources.StartPageControl_TitleEmpty;    
            }
            else {
                titleLabel.Text = Properties.Resources.StartPageControl_Title;
            }
            base.Refresh();

        }
    }

    // custom attributes
    public class FileEventArgs : System.EventArgs {

        private string message;
        private string _recentFile;

        public string RecentFile {
            get { return _recentFile; }
            set { _recentFile = value; }
        }

        public FileEventArgs(string m) {
            this.message = m;
        }

        public FileEventArgs(string m, string fileName)
            : this(m) {
            this._recentFile = fileName;
        }

        public string Message() {
            return message;
        }
    } 
}
